﻿using System.Collections;
using System.Collections.Specialized;
using System.IO;

namespace JCodeGen
{
    public abstract class CodeGenBase
    {
        private OrderedDictionary replacements = new OrderedDictionary();

        protected abstract void SetReplacement();

        public string GetTemplate(CodeGenFileTypes fileType)
        {
            return ResourcesManager.GetTemplate(this, fileType);
        }

        public void Generate(string filename, CodeGenFileTypes fileType)
        {
            string template = Generate(fileType);

            File.WriteAllText(filename, template);
        }

        public string Generate(CodeGenFileTypes fileType)
        {
            replacements.Clear();

            SetReplacement();

            string template = GetTemplate(fileType);

            string oldValue = string.Empty;
            string newValue = string.Empty;
            foreach (DictionaryEntry replacement in replacements)
            {
                oldValue = string.Format("${0}$", replacement.Key.ToString());
                newValue = replacement.Value.ToString();
                template = template.Replace(oldValue, newValue);
            }

            return template;
        }

        protected void AddReplacement(string key, string value)
        {
            replacements.Add(key, value);
        }
    }
}
